let app = getApp();
import {
    loginRq,
    registerPhone
} from "../../api/login/login.js"

Page({
    /**
     * 页面的初始数据
     */
    data: {
        IMG_NAME: app.IMG_NAME,
        getUserDataType: 'getPhoneNumber',
        openid: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (e) {
        // 清空所有缓存
        wx.clearStorageSync()
        // 自动登录获取openid
        this.autoLogin();
    },

    // 自动登录获取openid
    autoLogin() {
        let _this = this;
        wx.login({
            success(wxRes) {
                loginRq({
                    "appId": app.APPID,
                    "appSecret": app.APPSECRET,
                    "jsCode": wxRes.code
                }).then(res => {
                    if (res.code == 0) {
                    console.log('loginRq', res);
                    _this.setData({
                        openid: res.openid
                    })
                    // 判断用户是否注册
                    if (res.token) {
                        // 跳转首页
                        _this.jumpIndex(res.openid, res.user, res.token.token)
                    }
                    }else{
                        app.selfShowMsg(res.msg)
                    }
                })
            }
        })
    },

    // 获取手机号
    getPhoneNumber(e) {
        let _this = this;
        console.log('getPhoneNumber', e)
        registerPhone({
            "appId": app.APPID,
            "appSecret": app.APPSECRET,
            "code": e.detail.code,
            "openid": _this.data.openid
        }).then(res => {
            console.log('registerPhone', res);
            if (res.code == 0) {
                // 跳转首页
                _this.jumpIndex(res.openid, res.user, res.token.token)
            } else {
                app.selfShowMsg(res.msg)
            }
        })
    },
    // 跳转首页
    jumpIndex(openid, user, token) {
        // 清空所有缓存
        wx.clearStorageSync()
        // 存储用户信息
        wx.setStorageSync('openid', openid)
        wx.setStorageSync('user', user)
        wx.setStorageSync('userId', user.id)
        wx.setStorageSync('token', token)
        wx.setStorageSync('parkId', user.parkId)
        wx.setStorageSync('parkName', user.parkName)

        let currentPages = getCurrentPages();
        console.log('currentPages' , currentPages);
        // 大于一个页面就后退，否则直接跳转首页

            // 跳转首页
            wx.reLaunch({
              url: '/pages/index/newIndex/newIndex',
            })
    },

    /**
     * 授权登录
     */
    authorLogin: function (e) {
        console.log('getuserinfo', e.detail.rawData);
        let _this = this;
        if (e.detail.errMsg !== 'getUserInfo:ok') {
            return false;
        }
        wx.showLoading({
            title: "正在登录",
            mask: true
        });
        // 执行微信登录
        wx.login({
            success: function (res) {
                //发送请求
                wx.request({
                    header: {
                        'content-type': 'application/json'
                    },
                    method: 'POST',
                    url: app.DOMAIN_NAME + '/xiongan/login',
                    data: {
                        code: res.code,
                        rawData: e.detail.rawData,
                        encryptedData: e.detail.encryptedData,
                        ivStr: e.detail.iv,
                        signature: e.detail.signature
                    },
                    success(res) {
                        console.log(res);
                        if (res.data.code != 0) {
                            wx.showModal({
                                confirmText: '好的',
                                content: res.data.errmsg || '服务器开小差去了，请重试',
                                showCancel: false
                            });
                        } else if (res.data.code == 0) {
                            wx.setStorageSync('userId', res.data.userId)
                            wx.setStorageSync('token', res.data.token)
                            wx.hideLoading()
                            wx.reLaunch({
                                url: '/pages/index/newIndex/newIndex',
                            })
                        }
                    },
                    fail(ret) {}
                })
            }
        });
    },

    /**
     * 授权成功 跳转回原页面
     */
    navigateBack: function () {
        wx.navigateBack();
        // let currentPage = wx.getStorageSync('currentPage');
        // wx.redirectTo({
        //   url: '/' + currentPage.route + '?' + App.urlEncode(currentPage.options)
        // });
    },

})